/**
 * 存储
 */
define('appStore', ['vue', 'vuex', 'component/wxUtils','WxCore','restTemplate', 'appRoutes'], function (Vue, Vuex, { getHomeRoute, setTagNavListInLocalstorage, getTagNavListFromLocalstorage},wxCore,restTemplate, appRoutes) {
    Vue.use(Vuex)

    let showThisMenuEle = (item, access) => {
        if (item.meta && item.meta.access && item.meta.access.length) {
            if (hasOneOf(item.meta.access, access)) return true
            else return false
        } else return true
    }
    let getMenus = (list, access) => {
        let res = []
        for (var itemIndex in list) {
            var item = list[itemIndex]
            // console.log(item)
            if (!item.meta || (item.meta && !item.meta.hideInMenu)) {
                let obj = {
                    icon: (item.meta && item.meta.icon) || '',
                    name: item.name,
                    meta: item.meta
                }
                if (((item.children && item.children.length !== 0) || (item.meta && item.meta.showAlways)) && showThisMenuEle(item, access)) {
                    obj.children = getMenus(item.children, access)
                }
                if (item.meta && item.meta.href) obj.href = item.meta.href
                if (showThisMenuEle(item, access)) res.push(obj)
            }
        }
        return res
    }
    let getBreadCrumbList = (routeMetched) => {
        let res = routeMetched.filter(item => {
            return item.meta === undefined || !item.meta.hide
        }).map(item => {
            let obj = {
                icon: (item.meta && item.meta.icon) || '',
                name: item.name,
                meta: item.meta
            }
            return obj
        })
        res = res.filter(item => {
            return !item.meta.hideInMenu
        })
        return [{
            name: 'home',
            to: '/home/index'
        }, ...res]
    }

    const store = new Vuex.Store({
        namespaced: false,
        state: {
            userName: '',
            userId: '',
            avatorImgPath: '',
            token: '',
            access: '',
            local: '',
            tagNavList: [],
            breadCrumbList: [],
            homeRoute: getHomeRoute(appRoutes),
            // 已加载模块
            loadedModules: []
        },
        getters: {
            menuList: (state, getters, rootState) => getMenus(appRoutes, rootState.access),
            loadedModules: (state) => {
                return state.loadedModules
            }
        },
        mutations: {
            setLocal(state, lang) {
                state.local = lang
            },
            setBreadCrumb(state, routeMetched) {
                state.breadCrumbList = getBreadCrumbList(routeMetched)
            },
            setUserInfo(state,userInfo){
                state.userId=userInfo.userId;
                state.userName=userInfo.userName;
                state.token = userInfo.accessToken;
            },
            clearUserInfo(state){
                state.userId='';
                state.userName='';
                state.token = '';
            },
            setTagNavList(state, list) {
                if (list) {
                    state.tagNavList = [...list]
                    setTagNavListInLocalstorage([...list])
                } else state.tagNavList = getTagNavListFromLocalstorage()
            },
            addTag(state, item, type = 'unshift') {
                if (state.tagNavList.findIndex(tag => tag.name === item.name) < 0) {
                    if (type === 'push') state.tagNavList.push(item)
                    else state.tagNavList.unshift(item)
                    setTagNavListInLocalstorage([...state.tagNavList])
                }
            },
            // 添加加载过的Store模块
            addLoadedModule (state, moduleName) {
                state.loadedModules.push(moduleName)
            }
        },
        actions: {
            // 添已加载模块
            addLoadedModule ({ commit, state }, moduleName) {
                commit('addLoadedModule', moduleName)
            },
            getUserId({ commit, state }){
                return state.userId;
            },
            // 查询已加载模块
            findModule ({ commit, state }, module) {
                // console.log('findModule module', module)
                if (state.loadedModules.indexOf(module.name) > -1) {
                    return {name: module.name, loaded: true, storeModule: module.storeModule()}
                } else {
                    return {name: module.name, loaded: false, storeModule: module.storeModule()}
                }
            },
            handleLogOut({ commit, state }){
                commit('clearUserInfo');
                wxCore.removeSessionStoreItem('userinfo');
                return restTemplate.request({url:'/client/logout'});
            }
        }

    })
    return store
})
